'use strict';

const Controller = require('egg').Controller;
const api = require('../../config/api');

class PublicationController extends Controller {
  async index() {
    const { ctx } = this;
    let name = 'members';
    let res = {};
    const page = ctx.request.query.page;
    const id = ctx.request.query.id;
    const years = [];
    switch (ctx.path) {
      case '/members': {
        name = 'members';
        break;
      }
      case '/tan': {
        name = 'tan';
        // 教师详情界面
        res = await ctx.curl(`${api.basicUrl}/Teacher/getTeacherItem`, {
          method: 'GET',
          contentType: 'json',
          dataType: 'json',
          data: {
            teacherid: '4641831526ae4f9d9b83b013908823e6',
          },
        });
        break;
      }
      case '/faculty_staff': {
        //
        name = 'faculty_staff';
        if (id && id.length > 5) {
          // 教师详情界面
          res = await ctx.curl(`${api.basicUrl}/Teacher/getTeacherItem`, {
            method: 'GET',
            contentType: 'json',
            dataType: 'json',
            data: {
              teacherid: id,
            },
          });
          break;
        }
        res = await ctx.curl(`${api.basicUrl}/Teacher/getTeacherList`, {
          method: 'GET',
          contentType: 'json',
          dataType: 'json',
          data: {
            page: page || 1,
            pagesize: 12,
          },
        });
        break;
      }
      case '/fellows': {
        name = 'fellows';
        if (id && id.length > 5) {
          // 教师详情界面
          res = await ctx.curl(`${api.basicUrl}/Teacher/getTeacherItem`, {
            method: 'GET',
            contentType: 'json',
            dataType: 'json',
            data: {
              teacherid: id,
            },
          });
          break;
        }
        res = await ctx.curl(`${api.basicUrl}/Teacher/getTeacherList`, {
          method: 'GET',
          contentType: 'json',
          dataType: 'json',
          data: {
            page: page || 1,
            pagesize: 12,
          },
        });
        break;
      }
      case '/phd': {
        name = 'phd';
        res = await ctx.curl(`${api.basicUrl}/Student/getStudentList`, {
          method: 'GET',
          contentType: 'json',
          dataType: 'json',
          data: {
            page: page || 1,
            pagesize: 12,
          },
        });
        const data = res.data.rows || [];
        res.data.rows = data.filter(item => item.degree === 'BS');
        res.data.rows.sort((a, b) => Number(b.year) - Number(a.year));
        res.data.rows.forEach((item, index) => {
          if (index === 0 || index > 0 && years[index - 1] !== years[index]) {
            years.push(item.year);
          }
        });
        break;
      }
      case '/ms': {
        name = 'ms';
        res = await ctx.curl(`${api.basicUrl}/Student/getStudentList`, {
          method: 'GET',
          contentType: 'json',
          dataType: 'json',
          data: {
            page: page || 1,
            pagesize: 12,
          },
        });
        const data = res.data.rows || [];
        res.data.rows = data.filter(item => item.degree === 'MS');
        res.data.rows.sort((a, b) => Number(b.year) - Number(a.year));
        res.data.rows.forEach((item, index) => {
          if (index === 0 || index > 0 && years[index - 1] !== years[index]) {
            years.push(item.year);
          }
        });
        break;
      }
      case '/alumni': {
        name = 'alumni';
        res = await ctx.curl(`${api.basicUrl}/Student/getAlumniList`, {
          method: 'GET',
          contentType: 'json',
          dataType: 'json',
          data: {
            page: page || 1,
            pagesize: 12,
          },
        });
        const data = res.data.rows || [];
        // res.data.rows = data.filter(item => item.degree === 'MS');
        res.data.rows.sort((a, b) => Number(b.year) - Number(a.year));
        res.data.rows.forEach((item, index) => {
          if (index === 0 || index > 0 && years[index - 1] !== years[index]) {
            years.push(item.year);
          }
        });
        break;
      }
      default : {
        name = 'members';
        break;
      }
    }
    await ctx.render('members.html', {
      isDetail: !!id,
      name,
      pageData: res.data,
      years,
    });
  }
}

module.exports = PublicationController;
